package com.example.grpc.client.service;

import com.example.grpc.client.dto.UserResponseDTO;
import com.example.grpc.client.rpc.EndpointGrpcClient;
import com.example.grpc.client.rpc.GrpcChannelManager;
import io.grpc.ManagedChannel;
import org.springframework.stereotype.Component;
import proto.com.example.grpc.common.UserProto;
import proto.com.example.grpc.common.UserServiceGrpc;

@Component
public class UserServiceClient extends EndpointGrpcClient<UserServiceGrpc.UserServiceBlockingStub> {

    public UserServiceClient(GrpcChannelManager channelManager) {
        super(channelManager);
    }

    @Override
    protected String provideServiceName() {
        return "user-service";
    }

    @Override
    protected int provideServicePort() {
        return 20153;
    }

    @Override
    protected UserServiceGrpc.UserServiceBlockingStub createStub(ManagedChannel channel) {
        return UserServiceGrpc.newBlockingStub(channel);
    }

    public UserResponseDTO getUser(int userId) {
        UserProto.UserRequest request = UserProto.UserRequest.newBuilder()
                .setUserId(userId)
                .build();
        UserProto.UserResponse user = getStub().getUser(request);
        UserResponseDTO from = UserResponseDTO.from(user);
        return from;
    }
}